package com.wanxi.order.api;


import com.wanxi.domain.vo.Paging;
import com.wanxi.orderApi.OrderFeign;
import com.wanxi.domain.code.ResultCode;
import com.wanxi.domain.dto.OrderStatus;
import com.wanxi.domain.vo.OrderQueryVO;
import com.wanxi.domain.vo.OrderVO;
import com.wanxi.domain.vo.PlaceOrderVO;

import com.wanxi.order.service.OrderService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author LiuYuRong
 * @Date 2022/1/11 14:29
 * @Version 1.0
 * @Description
 */
@RestController
@RequestMapping("/order/api/")
@Api(tags = "订单API设计")
public class OrderApi  implements OrderFeign {
    @Autowired
    private OrderService core;

    @GetMapping("customer/getCustomerOrdersById/")
    @Override
    public List<OrderVO> getCustomerOrdersById( @RequestParam("id") Long id, Paging page) {
        OrderQueryVO queryVO=new OrderQueryVO();
        queryVO.setPage(page.getPage());
        queryVO.setPageSize(page.getPageSize());
        return core.getOrders(id, 0, queryVO);
    }


    @GetMapping("/customer/getShopOrdersById/")
    @Override
    public List<OrderVO> getShopOrdersById( OrderQueryVO vo) {
        return core.getOrders(vo.getRoleId(),1,vo);
    }

    @GetMapping("/enterprise/getEnterpriseOrdersById/")
    @Override
    public List<OrderVO> getEnterpriseOrdersById( Long id,Paging paging) {
        OrderQueryVO queryVO=new OrderQueryVO();
        queryVO.setPage(paging.getPage());
        queryVO.setPageSize(paging.getPageSize());
        return core.getOrders(id,2,queryVO);
    }



    @PutMapping("/pay/{orderId}")
    @Override
    public ResultCode payOrder(@PathVariable long orderId) {
        int i = core.payOrder(orderId);
        if (i==1) return ResultCode.SUCCESS;
        else return ResultCode.FAILED;

    }

    @PostMapping("/placeOrder/")
    @Override
    public ResultCode addOrder(@RequestBody PlaceOrderVO order) {
        int i = core.addOrder(order);
        if (i==1) return ResultCode.SUCCESS;
        else return ResultCode.FAILED;
    }

    @PutMapping("/send/{orderId}")
    @Override
    public ResultCode send(@PathVariable long orderId) {
         return core.changeOrderStatus(orderId, OrderStatus.send);
    }

    @PutMapping("/received/{orderId}")
    @Override
    public ResultCode receive(@PathVariable long orderId) {
        return core.changeOrderStatus(orderId,OrderStatus.received);
    }

    @PutMapping("/order/api/autocancel/{orderId}")
    @Override
    public ResultCode autoCancel(@PathVariable long orderId) {
        return core.changeOrderStatus(orderId,OrderStatus.autoCanceled);
    }


    @PutMapping("/order/api/handlecancel/{orderId}")
    @Override
    public ResultCode handleCancel(@PathVariable long orderId) {
        return core.changeOrderStatus(orderId,OrderStatus.handleCanceled);
    }



    @DeleteMapping("/delete/{orderId}")
    @Override
    public ResultCode deleteOrderById(@PathVariable long orderId) {
         return core.deleteOrder(orderId);
    }

    @PostMapping("/beforeSubmitOrder/")
    @Override
    public Map<String, Object> beforeSubmitOrder(PlaceOrderVO orderVO) {
        return core.beforeSubmitOrder(orderVO);
    }


}
